package com.be.beadmin.basicData.init;

import cn.hutool.core.util.ObjectUtil;
import com.be.beadmin.basicData.entity.Role;
import com.be.beadmin.basicData.entity.User;
import com.be.beadmin.basicData.repository.RoleRepository;
import com.be.beadmin.basicData.repository.UserRepository;
import com.be.beadmin.common.constant.SystemInitConstant;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

/**
 * @author chenwei
 * @date 2/23/2023 4:31 PM
 */
@Component
@Order(value = 3)
public class BasicDataInit implements ApplicationRunner {
    @Autowired
    private UserRepository userRepository;


    @Autowired
    private RoleRepository roleRepository;



    @Value("${beadmin.init.auto}")
    private String state = "close";



    @Override
    public void run(ApplicationArguments args) throws Exception {
        if(state != null && SystemInitConstant.INIT_STATE_OPEN.equals(state)) {
            // 创建系统默认用户

            User userQuery = userRepository.findUserByNameAndDelete(SystemInitConstant.ADMIN, 0);

            // 如果系统里存在 就没有必要添加
            if(ObjectUtil.isEmpty(userQuery)) {
                User user = new User();

                user.setName(SystemInitConstant.ADMIN);
                user.setPassword(SystemInitConstant.PASSWORD);
                user.setSystem(true);
                user = userRepository.save(user);


                // 添加管理员角色
                Role adminRole =addRole("系统管理员",SystemInitConstant.SYSTEM_ROLE_ADMIN);
                user.getRoles().add(adminRole);

                // 游客
                Role guestRole = addRole("游客", SystemInitConstant.SYSTEM_ROLE_GUEST);
                user.getRoles().add(guestRole);


                Role purchaseManagerRole = addRole("采购经理", SystemInitConstant.SYSTEM_ROLE_PURCHASE_MANAGER);
                user.getRoles().add(purchaseManagerRole);


                Role purchaseOperator = addRole("采购员", SystemInitConstant.SYSTEM_ROLE_PURCHASE_OPERATOR);
                user.getRoles().add(purchaseOperator);

                Role techManager = addRole("技术经理", SystemInitConstant.SYSTEM_ROLE_TECH_MANAGER);
                user.getRoles().add(techManager);


                Role techOperator = addRole("技术经理", SystemInitConstant.SYSTEM_ROLE_TECH_OPERATOR);
                user.getRoles().add(techOperator);


                Role manufactManager = addRole("生产经理", SystemInitConstant.SYSTEM_ROLE_MANUFACT_MANAGER);
                user.getRoles().add(manufactManager);

                Role manufactOperator = addRole("操作工", SystemInitConstant.SYSTEM_ROLE_MANUFACT_OPERATOR);
                user.getRoles().add(manufactOperator);

                Role logistcManager = addRole("物流经理", SystemInitConstant.SYSTEM_ROLE_LOGISTIC_MANAGER);
                user.getRoles().add(logistcManager);

                Role logistcOperator = addRole("物流经理", SystemInitConstant.SYSTEM_ROLE_LOGISTIC_OPERATOR);
                user.getRoles().add(logistcOperator);

                Role pickOperator = addRole("分包", SystemInitConstant.SYSTEM_ROLE_PICK_OPERATOR);
                user.getRoles().add(pickOperator);

                Role packOperator = addRole("物流经理", SystemInitConstant.SYSTEM_ROLE_PACK_OPERATOR);
                user.getRoles().add(packOperator);

                Role projectManger = addRole("项目经理", SystemInitConstant.SYSTEM_ROLE_PROJECT_MANAGER);
                user.getRoles().add(projectManger);

                // 最后保存数据
                userRepository.save(user);

                System.out.println("用户初始化数据已经导入");

            }

        } else {
//            System.out.println("初始化失败");
        }

    }


    public Role addRole(String roleName, String roleCode) {
        Role role = new Role();
        role.setName(roleName);
        role.setCode(roleCode);
        role.setSystem(true);
        return roleRepository.save(role);
    }
}
